/**
 * @format
 * @Author: TC
 * @Date: 2025-06-12 20:08:17
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-28 16:43:56
 */

import { _decorator, Enum, v3, Vec3 } from "cc";
import { app } from "db://assets/FrameWork/App";
import { nsBulletConst } from "../../../Const/BulletConst";
import { nsBulletInterface } from "./BulletInterface";
import { BasePoolItem } from "db://assets/FrameWork/Module/Component/BasePoolItem";
import { BulletMove } from "./BulletMove";
import { ViewLogic } from "../../../Module/Game/Logic/ViewLogic";
import { nsSkillConfig } from "../../../Config/SkillConfig";

const { ccclass, property } = _decorator;

@ccclass
export class BulletBase extends BasePoolItem {
    @property({
        tooltip: "是否发射抛出物 对准目标方向自动旋转角度",
    })
    autoAngle: boolean = true;

    protected _data: nsBulletInterface.IBullet = null;
    /**
     * 子弹配置
     */
    private _cfg: CfgInterface.IBullet = null;
    public get cfg(): CfgInterface.IBullet {
        return this._cfg;
    }

    private _move: BulletMove = new BulletMove(this);
    public get move(): BulletMove {
        return this._move;
    }

    /**
     * 重置
     */
    private _reset(): void {
        this._cfg = this._data.cfg;
        app.tickMgr.add(this);
    }
    tick(dt: number) {
        this._move.tick(dt);
        this._checkAutoDestroy();
    }

    /**
     * 检测超屏删除
     */
    private _checkAutoDestroy(): void {
        if (ViewLogic.offScreen(this.node.position.x, this.node.position.y, 400)) {
            this.destroySelf();
        }
    }

    /**
     * 删除自身
     */
    public destroySelf(): void {
        this.recycle();
    }

    /**
     * 从对象池取出
     */
    onGet(): void {
        super.onGet();
        this._reset();
        app.tickMgr.add(this);
    }

    /**
     * 放入对象池
     */
    onPut(): void {
        this._release();
        super.onPut();
    }
    public recycle(): void {
        app.tickMgr.remove(this);
        //回收到对象池
        if (!this._data) {
            app.log.error(this);
            return;
        }
        app.poolMgr.putItem(this._cfg.id.toString(), this.node);
        this._data = null;
    }

    /**
     * 获取发射点在子弹层级的坐标
     * @returns
     */
    public getCastPos(): Vec3 {
        return v3();
    }

    /**
     * 释放(对象池回收)
     */
    private _release(): void {
        app.tickMgr.remove(this);
    }
}
